package com.bw.unit3.view.fragment

import android.view.View
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bw.unit3.Const
import com.bw.unit3.R
import com.bw.unit3.base.BaseFragment
import com.bw.unit3.bean.GoodsBean
import com.bw.unit3.databinding.FragmentHomeBinding
import com.bw.unit3.view.adapter.HomeGoodsAdapter
import com.bw.unit3.viewmodel.HomeViewModel
import com.bw.unit3.viewmodel.LoginViewModel
import com.jcodecraeer.xrecyclerview.XRecyclerView.LoadingListener
import com.stx.xhb.androidx.XBanner
import com.stx.xhb.androidx.XBanner.XBannerAdapter
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter.OnItemClickListener

/**
 * create an instance of this fragment.
 */
class HomeFragment : BaseFragment<HomeViewModel, FragmentHomeBinding>(R.layout.fragment_home) {

    val typeId = 0
    var page = 1
    val size = 10//一页请求10条数据

    private var mData = mutableListOf<GoodsBean>()
    private lateinit var homeAdapter: HomeGoodsAdapter

    //    var str:String? = null
    override fun initData() {
        //处理banner
        viewModel.liveDataBanner.observe(this) {
            dataBinding.banner.setData(it.data, null)
            dataBinding.banner.setAutoPlayAble(true)
            dataBinding.banner.setAutoPlayTime(1200)
            dataBinding.banner.setmAdapter(object : XBannerAdapter {
                override fun loadBanner(banner: XBanner?, model: Any?, view: View?, position: Int) {
                    activity?.let { it1 ->
                        Glide.with(it1).load(it.data[position].imagePath).into(view as ImageView)
                    }
                }
            })
        }

        //处理列表
        viewModel.liveDataGoods.observe(this) {
            //当页面==1，请求第一页数据时，需要情况当前的容器
            if (page == 1) {
                mData.clear()
            }
            mData.addAll(it.data)
            homeAdapter.notifyDataSetChanged()
            //刷新完成，结束刷新动画
            dataBinding.rv.refreshComplete()
        }

//        str?.let { it.length }
    }

    override fun initView() {
        //获取banner列表
        viewModel.getBanner()
        //获取商品列表
        viewModel.getGoodsList(typeId, page, size)

        //水平滑动列表
//        dataBinding.rv.layoutManager = LinearLayoutManager(activity,LinearLayoutManager.HORIZONTAL,false)
        dataBinding.rv.layoutManager = GridLayoutManager(activity, 2)
        homeAdapter = HomeGoodsAdapter(activity, R.layout.item_goods_layout, mData)
        dataBinding.rv.adapter = homeAdapter

        //实现刷新和加载更多
        dataBinding.rv.setLoadingListener(object : LoadingListener {
            override fun onRefresh() {
                //刷新，重新请求第一页数据
                page = 1
                viewModel.getGoodsList(typeId, page, size)
            }

            override fun onLoadMore() {
                //加载更多，请求下一页数据
                page++
                viewModel.getGoodsList(typeId, page, size)
            }
        })


        homeAdapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(p0: View?, p1: RecyclerView.ViewHolder?, p2: Int) {
                ToastUtils.showShort("点击下标为$p2")
                ARouter.getInstance().build(Const.PATH_GOODS_DETAIL)
//                    .withString("title", mData[p2 - 1].goods_desc)
                    .withInt("goodsId", mData[p2 - 1].id)
                    .navigation()
            }

            override fun onItemLongClick(
                p0: View?,
                p1: RecyclerView.ViewHolder?,
                p2: Int
            ): Boolean {
                return true
            }

        })

    }


}